var userInfoData = {};

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("userInfoThead", userInfoTable);
    $("#change").show();
    $("#save").hide();
	search();
})
function search() {
	$ajax('searchUserInfo', '', searchUserInfo);
}
function searchUserInfo(data){
	userInfoData = data && data.userInfo || [];
	createDetailBody("userInfoTbody", userInfoTable, userInfoData, 1, true);
	setFormDisabled("saveForm");
}
function change() {
	deleteFlag = "update";
	$("#userID").attr("disabled", false);
	$("#userID").attr("readOnly", true);
    $("#userName").attr("disabled", false);
    $("#mailAddress").attr("disabled", false);
    $("#phone").attr("disabled", false);
    $("#save").show();
    $("#change").hide();
}
function saveUser(data){
	if (deleteFlag == "update"){
		var result = data && data.result;
		if (result == "Success"){
			showMessge("修改成功");
		} else if (result == "NotFound"){
			showMessge("修改失败");	
		} else{
			showMessge("修改异常")
		}
	    $("#save").hide();
	    $("#change").show();
		search();
	} else if(deleteFlag == "updatePassword"){
		var result = data && data.result;
		if (result == "Success"){
			showMessge("修改成功！");
			$("#updatePassWord").modal("hide");
		} else if (result == "NotFound"){
			showMessge("修改失败，该用户不存在或原密码错误！");	
		} else{
			showMessge("修改异常，联系管理员！")
		}
		formReset("updatePasswordForm");
		search();
	}
}
function getUrl(){
	if(deleteFlag == "update"){
		return 'updateUserInfo';
	} else if(deleteFlag == "updatePassword"){
		return 'updatePassword';
	}
}
function confirm(){
	if(deleteFlag == "update"){
		return checkForm("saveForm", userInfoTable);
	} else if(deleteFlag == "updatePassword"){
		return checkForm("updatePasswordForm");
	}
}
function updatePassword() {
	deleteFlag = "updatePassword";
    $("#updatePassWord").modal("show");
}
function sureUpdatePassword(){
	var newPassword = $("#newPassword").val();
	var surePassword = $("#surePassword").val();
	if (newPassword == surePassword) {
		var checkbox = getCheckbox("chk");
		var index = checkbox && checkbox.row_index;
		$ajax(getUrl, {UserID: userInfoData && userInfoData.userID, OldPassword: $.md5($("#oldPassword").val()), Password: $.md5(newPassword)}, saveUser, confirm);
	} else {
		showMessge("两次输入密码不一致，请重新输入！");
		formReset("updatePasswordForm");
	}
}